<?php
class bibtexHelper extends AppHelper {

	var $_pattern = array("\r\n" => "\s", '"' => "'");
	var $_authorPattern = array("\r\n" => "\s", '"' => "'", ',' => "");

	/**
	 * BibTeXのタイプを調査する
	 *
	 * @param $dcvalue
	 * @param (arr) $BibTypes DSPACE の文献タイプ(key) と BibTeX のタイプ(value)をマッピングした連想配列
	 * @return unknown_type
	 */
	function type( $dcvalue, $BibTypes ) {
		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			if ( $value['dc_type_id'] == 94 ) { // 九大拡張 Junii+用資料種別
				return $this->_searchVal( $value['text_value'], $BibTypes );
				break;
			}
		}
	}

	/**
	 * @param $str
	 * @param (arr) $BibTypes DSPACE の文献タイプ(key) と BibTeX のタイプ(value)をマッピングした連想配列
	 * @return (str) $val BibTeX ヘッダ
	 */
	function _searchVal( $str, $BibTypes ) {
		if ( empty($str) ) return false;
		foreach ( $BibTypes AS $key => $val ) {
			if ( $str == $key ) {
				return $val;
				break;
			}
		}
	}

	/**
	 * @param $dcvalue
	 * @return (str) $searchkey ex) QIR:/2324/3652,\r\n
	 */
	function searchkey( $dcvalue ) {
		$searchkey = SEARCHKEY; // BibTeX 格納用変数
		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			switch( $value['dc_type_id'] ) {
				case 30; // Uniform Resource Identifier
					$tmp = substr($value['text_value'],27,4); //ex) http://hdl.handle.net/2324/3652
					$tmp = intval($tmp);
					if ( empty( $tmp ) ) {
						LogError("intval({$tmp});" . __FILE__);
					}
					$searchkey .= "{$tmp},\r\n";
					break;
			}
		}
		return $searchkey;
	}

	function article( $dcvalue ) {
		$author  = ""; // BibTeX 格納用変数
		$journal = "";
		$title   = "";
		$year    = "";
		$note    = "";
		$number  = "";
		$pages   = "";
		$volume  = "";

		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			switch( $value['dc_type_id'] ) {
				case 3 :
					if ( !empty($author) ) { // 文字列が空白でなければ複数
						$author .= " and " . $this->_strRep($value['text_value'], $this->_authorPattern );
					} else {
						$author .= $this->_strRep($value['text_value'], $this->_authorPattern );
					}
					break;
				case 95 :
					$journal .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 75 :
					$title   .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 19 : // 最古の年度が出力される
					if ( empty($year) ) { // 文字列が空白でなければ複数
						$year .= $this->_strRep($value['text_value'], $this->_pattern );
					}
					break;
				case 31 :
					$note    .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 97 :
					$number  .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 98 :
					$pages   .= $this->_strRep($value['text_value'], $this->_pattern ); // 開始ページ
					break;
				case 99 :
					$pages   .= "-" . $this->_strRep($value['text_value'], $this->_pattern ); // 終了ページ
					break;
				case 96 :
					$volume  .= $value['text_value'];
					break;
			}
		}

		$bibtex =
			"author={"  . $author  . "}," . "\r\n" .
			"journal={" . $journal . "}," . "\r\n" .
			"title={"   . $title   . "}," . "\r\n" .
			"year={"    . $year    . "}," . "\r\n" .
			"note={"    . $note    . "}," . "\r\n" .
			"number={"  . $number  . "}," . "\r\n" .
			"pages={"   . $pages   . "}," . "\r\n" .
			"volume={"  . $volume  . "}," . "\r\n"
		;

		return $bibtex;
	}

	function phdthesis( $dcvalue ) {
		$author = ""; // BibTeX 格納用変数
		$school = "";
		$title  = "";
		$year   = "";
		$note   = "";
		$type   = "";

		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			switch( $value['dc_type_id'] ) {
				case 3 :
					if ( !empty($author) ) { // 文字列が空白でなければ複数
						$author .= " and " . $this->_strRep($value['text_value'], $this->_authorPattern );
					} else {
						$author .= $this->_strRep($value['text_value'], $this->_authorPattern );
					}
					break;
				case 44 :
					$school .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 75 :
					$title  .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 19 : // 最古の年度が出力される
					if ( empty($year) ) { // 文字列が空白でなければ複数
						$year .= $this->_strRep($value['text_value'], $this->_pattern );
					}
					break;
				case 31 :
					$note   .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 78 :
					$type   .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
			}
		}

		$bibtex =
			"author={" . $author . "}," . "\r\n" .
			"school={" . $school . "}," . "\r\n" .
			"title={"  . $title  . "}," . "\r\n" .
			"year={"   . $year   . "}," . "\r\n" .
			"note={"   . $note   . "}," . "\r\n" .
			"type={"   . $type   . "}," . "\r\n"
		;

		return $bibtex;
	}

	function inproceeding( $dcvalue ) {
		$author    = ""; // BibTeX 格納用変数
		$booktitle = "";
		$title     = "";
		$year      = "";
		$volume    = "";
		$number    = "";
		$note      = "";
		$pages     = "";
		$publisher = "";
		$series    = "";

		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			switch( $value['dc_type_id'] ) {
				case 3 :
					if ( !empty($author) ) { // 文字列が空白でなければ複数
						$author .= " and " . $this->_strRep($value['text_value'], $this->_authorPattern );
					} else {
						$author .= $this->_strRep($value['text_value'], $this->_authorPattern );
					}
					break;
				case 95 :
					$booktitle .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 75 :
					$title     .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 19 : // 最古の年度が出力される
					if ( empty($year) ) { // 文字列が空白でなければ複数
						$year .= $this->_strRep($value['text_value'], $this->_pattern );
					}
					break;
				case 31 :
					$note      .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 96 :
					$volume    .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 97 :
					$number    .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 98 :
					$pages     .= $this->_strRep($value['text_value'], $this->_pattern ); // 開始ページ
					break;
				case 99 :
					$pages     .= "-" . $this->_strRep($value['text_value'], $this->_pattern ); // 終了ページ
					break;
				case 44 :
					$publisher .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 50 :
					$series    .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
			}
		}

		$bibtex =
			"author={"    . $author    . "}," . "\r\n" .
			"booktitle={" . $booktitle . "}," . "\r\n" .
			"title={"     . $title     . "}," . "\r\n" .
			"year={"      . $year      . "}," . "\r\n" .
			"volume={"    . $volume    . "}," . "\r\n" .
			"number={"    . $number    . "}," . "\r\n" .
			"note={"      . $note      . "}," . "\r\n" .
			"pages={"     . $pages     . "}," . "\r\n" .
			"publisher={" . $publisher . "}," . "\r\n" .
			"series={"    . $series    . "}," . "\r\n"
		;

		return $bibtex;
	}

	function book( $dcvalue ) {
		$author    = ""; // BibTeX 格納用変数
		$title     = "";
		$publisher = "";
		$volume    = "";
		$number    = "";
		$note      = "";
		$series    = "";

		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			switch( $value['dc_type_id'] ) {
				case 3 :
					if ( !empty($author) ) { // 文字列が空白でなければ複数
						$author .= " and " . $this->_strRep($value['text_value'], $this->_authorPattern );
					} else {
						$author .= $this->_strRep($value['text_value'], $this->_authorPattern );
					}
					break;
				case 75 :
					$title     .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 44 :
					$publisher .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 96 :
					$volume    .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 97 :
					$number    .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 31 :
					$note      .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 50 :
					$series    .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
			}
		}

		$bibtex =
			"author={"    . $author    . "}," . "\r\n" .
			"title={"     . $title     . "}," . "\r\n" .
			"publisher={" . $publisher . "}," . "\r\n" .
			"volume={"    . $volume    . "}," . "\r\n" .
			"number={"    . $number    . "}," . "\r\n" .
			"note={"      . $note      . "}," . "\r\n" .
			"series={"    . $series    . "}," . "\r\n"
		;

		return $bibtex;
	}

	function techreport( $dcvalue ) {
		$author      = ""; // BibTeX 格納用変数
		$title       = "";
		$institution = ""; // publisher を入れる
		$year        = "";
		$type        = "";

		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			switch( $value['dc_type_id'] ) {
				case 3 :
					if ( !empty($author) ) { // 文字列が空白でなければ複数
						$author .= " and " . $this->_strRep($value['text_value'], $this->_authorPattern );
					} else {
						$author .= $this->_strRep($value['text_value'], $this->_authorPattern );
					}
					break;
				case 75 :
					$title       .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 44 : // publisher を入れる
					$institution .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 19 : // 最古の年度が出力される
					if ( empty($year) ) { // 文字列が空白でなければ複数
						$year .= $this->_strRep($value['text_value'], $this->_pattern );
					}
					break;
				case 78 :
					$type        .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
			}
		}

		$bibtex =
			"author={"      . $author      . "}," . "\r\n" .
			"title={"       . $title       . "}," . "\r\n" .
			"institution={" . $institution . "}," . "\r\n" .
			"year={"        . $year        . "}," . "\r\n" .
			"type={"        . $type        . "}," . "\r\n"
		;

		return $bibtex;
	}

	function unpublished( $dcvalue ) {
		$author = ""; // BibTeX 格納用変数
		$title  = "";
		$note   = "";
		$year   = "";

		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			switch( $value['dc_type_id'] ) {
				case 3 :
					if ( !empty($author) ) { // 文字列が空白でなければ複数
						$author .= " and " . $this->_strRep($value['text_value'], $this->_authorPattern );
					} else {
						$author .= $this->_strRep($value['text_value'], $this->_authorPattern );
					}
					break;
				case 75 :
					$title .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 31 :
					$note  .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 19 : // 最古の年度が出力される
					if ( empty($year) ) { // 文字列が空白でなければ複数
						$year .= $this->_strRep($value['text_value'], $this->_pattern );
					}
					break;
			}
		}

		$bibtex =
			"author={" . $author . "}," . "\r\n" .
			"title={"  . $title  . "}," . "\r\n" .
			"note={"   . $note   . "}," . "\r\n" .
			"year={"   . $year   . "}," . "\r\n"
		;

		return $bibtex;
	}

	function manual( $dcvalue ) {
		$title  = ""; // BibTeX 格納用変数
		$author = "";
		$note   = "";
		$year   = "";

		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			switch( $value['dc_type_id'] ) {
				case 75 :
					$title .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 3 :
					if ( !empty($author) ) { // 文字列が空白でなければ複数
						$author .= " and " . $this->_strRep($value['text_value'], $this->_authorPattern );
					} else {
						$author .= $this->_strRep($value['text_value'], $this->_authorPattern );
					}
					break;
				case 31 :
					$note  .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 19 : // 最古の年度が出力される
					if ( empty($year) ) { // 文字列が空白でなければ複数
						$year .= $this->_strRep($value['text_value'], $this->_pattern );
					}
					break;
			}
		}

		$bibtex =
			"title={"  . $title  . "}," . "\r\n" .
			"author={" . $author . "}," . "\r\n" .
			"note={"   . $note   . "}," . "\r\n" .
			"year={"   . $year   . "}," . "\r\n"
		;

		return $bibtex;
	}

	function misc( $dcvalue ) {
		$author = ""; // BibTeX 格納用変数
		$title  = "";
		$year   = "";
		$note   = "";

		if ( empty($dcvalue) ) return false;
		foreach( $dcvalue as $key => $value ) {
			if ( empty($value['dc_type_id']) ) continue;
			switch( $value['dc_type_id'] ) {
				case 3 :
					if ( !empty($author) ) { // 文字列が空白でなければ複数
						$author .= " and " . $this->_strRep($value['text_value'], $this->_authorPattern );
					} else {
						$author .= $this->_strRep($value['text_value'], $this->_authorPattern );
					}
					break;
				case 75 :
					$title .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
				case 19 : // 最古の年度が出力される
					if ( empty($year) ) { // 文字列が空白でなければ複数
						$year .= $this->_strRep($value['text_value'], $this->_pattern );
					}
					break;
				case 31 :
					$note  .= $this->_strRep($value['text_value'], $this->_pattern );
					break;
			}
		}

		$bibtex =
			"author={" . $author . "}," . "\r\n" .
			"title={"  . $title  . "}," . "\r\n" .
			"year={"   . $year   . "}," . "\r\n" .
			"note={"   . $note   . "}," . "\r\n"
		;

		return $bibtex;
	}

	/**
	 * 文字列置換関数
	 *
	 * @param  (string) $str    : 置換文字列
	 * @param  (array)  $target : 置換パターン ex : "置換対象=>置換文字"の連想配列
	 * @return (string) true    : 置換済み文字列  false : false
	 */
	function _strRep( $str, $target ) {
		if ( is_array( $target ) && !empty( $target ) ) {
			foreach ( $target AS $key => $val ) {
				$str = preg_replace("/{$key}/",$val, $str );
			}
		}
		return $str;
	}
}
?>